#include "Inventory.h"


Inventory::Inventory(int m)
{
	backPackSize = m;
}


Inventory::~Inventory()
{
}


Inventory::Inventory(const Inventory& invent)
{
	this->setBackPackSize(invent.getBackPackSize());

	this->weapons.clear();
	this->weapons.reserve(invent.weapons.size());
	for (int i = 0; i != invent.weapons.size(); i++)
	{
		this->weapons.push_back(invent.getWeapon(i));
	}

	this->armors.clear();
	this->armors.reserve(invent.armors.size());
	for (int j = 0; j != invent.armors.size(); j++)
	{
		this->armors.push_back(invent.getArmor(j));
	}

	this->books.clear();
	this->books.reserve(invent.books.size());
	for (int k = 0; k != invent.books.size(); k++)
	{
		this->books.push_back(invent.getBook(k));
	}

	this->food.clear();
	this->food.reserve(invent.food.size());
	for (int l = 0; l != invent.food.size(); l++)
	{
		this->food.push_back(invent.getFood(l));
	}

	this->potions.clear();
	this->potions.reserve(invent.potions.size());
	for (int m = 0; m != invent.potions.size(); m++)
	{
		this->potions.push_back(invent.getPotion(m));
	}
}


Inventory& Inventory::operator=(const Inventory& invent)
{
	if (this != &invent)
	{
		this->setBackPackSize(invent.getBackPackSize());

		this->weapons.clear();
		this->weapons.reserve(invent.weapons.size());
		for (int i = 0; i != invent.weapons.size(); i++)
		{
			this->weapons.push_back(invent.getWeapon(i));
		}

		this->armors.clear();
		this->armors.reserve(invent.armors.size());
		for (int j = 0; j != invent.armors.size(); j++)
		{
			this->armors.push_back(invent.getArmor(j));
		}

		this->books.clear();
		this->books.reserve(invent.books.size());
		for (int k = 0; k != invent.books.size(); k++)
		{
			this->books.push_back(invent.getBook(k));
		}

		this->food.clear();
		this->food.reserve(invent.food.size());
		for (int l = 0; l != invent.food.size(); l++)
		{
			this->food.push_back(invent.getFood(l));
		}

		this->potions.clear();
		this->potions.reserve(invent.potions.size());
		for (int m = 0; m != invent.potions.size(); m++)
		{
			this->potions.push_back(invent.getPotion(m));
		}
	}
	return *this;
}


//accessors
int Inventory::getBackPackSize() const
{
	return backPackSize;
}


int Inventory::getOccupiedInventory() const
{
	return (weapons.size() + armors.size() + books.size() + food.size() + potions.size());
}


bool Inventory::hasFreeSpace() const
{
	if (this->getBackPackSize() > this->getOccupiedInventory())
	{
		return true;
	}
	else
	{
		return false;
	}
}


Weapon& Inventory::getWeapon(int pos) const
{
	Weapon* tmpWeapon = new Weapon(weapons[pos]);
	return *tmpWeapon;
}


Armor& Inventory::getArmor(int pos) const
{
	Armor* tmpArmor = new Armor(armors[pos]);
	return *tmpArmor;
}


Book& Inventory::getBook(int pos) const
{
	Book* tmpBook = new Book(books[pos]);
	return *tmpBook;
}


Food& Inventory::getFood(int pos) const
{
	Food* tmpFood = new Food(food[pos]);
	return *tmpFood;
}


Potion& Inventory::getPotion(int pos) const
{
	Potion* tmpPotion = new Potion(potions[pos]);
	return *tmpPotion;
}


//mutators
void Inventory::setBackPackSize(int m)
{
	this->backPackSize = m;
}


void Inventory::setWeapon(const Weapon& w)
{
	if (this->hasFreeSpace())
	{
		this->weapons.push_back(w);
	}
}


void Inventory::setWeapon(const Weapon& w, int pos)
{
	if (this->hasFreeSpace())
	{
		vector<Weapon>::iterator iter;
		iter = this->weapons.begin();
		iter = this->weapons.insert(iter + pos, w);
	}
}


void Inventory::deleteWeapon(int pos)
{
	if (!this->weapons.empty())
	{
		vector<Weapon>::iterator iter;
		iter = this->weapons.begin();
		this->weapons.erase(iter + pos);
	}
}


void Inventory::setArmor(const Armor& a)
{
	if (this->hasFreeSpace())
	{
		this->armors.push_back(a);
	}
}


void Inventory::setArmor(const Armor& a, int pos)
{
	if (this->hasFreeSpace())
	{
		vector<Armor>::iterator iter;
		iter = this->armors.begin();
		iter = this->armors.insert(iter + pos, a);
	}
}


void Inventory::deleteArmor(int pos)
{
	if (!this->weapons.empty())
	{
		vector<Armor>::iterator iter;
		iter = this->armors.begin();
		this->armors.erase(iter + pos);
	}
}


void Inventory::setBook(const Book& b)
{
	if (this->hasFreeSpace())
	{
		this->books.push_back(b);
	}
}


void Inventory::setBook(const Book& b, int pos)
{
	if (this->hasFreeSpace())
	{
		vector<Book>::iterator iter;
		iter = this->books.begin();
		iter = this->books.insert(iter + pos, b);
	}
}


void Inventory::deleteBook(int pos)
{
	if (!this->books.empty())
	{
		vector<Book>::iterator iter;
		iter = this->books.begin();
		this->books.erase(iter + pos);
	}
}


void Inventory::setFood(const Food& f)
{
	if (this->hasFreeSpace())
	{
		this->food.push_back(f);
	}
}


void Inventory::setFood(const Food& f, int pos)
{
	if (this->hasFreeSpace())
	{
		vector<Food>::iterator iter;
		iter = this->food.begin();
		iter = this->food.insert(iter + pos, f);
	}
}


void Inventory::deleteFood(int pos)
{
	if (!this->food.empty())
	{
		vector<Food>::iterator iter;
		iter = this->food.begin();
		this->food.erase(iter + pos);
	}
}


void Inventory::setPotion(const Potion& p)
{
	if (this->hasFreeSpace())
	{
		this->potions.push_back(p);
	}
}


void Inventory::setPotion(const Potion& p, int pos)
{
	if (this->hasFreeSpace())
	{
		vector<Potion>::iterator iter;
		iter = this->potions.begin();
		iter = this->potions.insert(iter + pos, p);
	}
}


void Inventory::deletePotion(int pos)
{
	if (!this->potions.empty())
	{
		vector<Potion>::iterator iter;
		iter = this->potions.begin();
		this->potions.erase(iter + pos);
	}
}